package com.epalmpay.job;

import java.util.Date;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import com.epalmpay.enumdef.CommonEnum.RunStatus;
import com.epalmpay.enumdef.CommonEnum.TaskType;
import com.epalmpay.service.operator.ISysLogService;
import com.epalmpay.util.DateUtil;
import com.epalmpay.util.Logger;

@Component
public class ProfitDayCalculateJob {
     
	@Resource
	private ISysLogService sysLogService;
	//@Resource
	//private IProfitStatisService profitStatisService;
	
	protected Logger logger = Logger.getLogger(this.getClass());	
	
	/**
	 * 分润日计算(暂修改为实时异步计算，不跑批)
	 * 每天凌晨4点、10点、16点、22点执行一次
	 * 对有失败的任务可通过
	 */
	//@Scheduled(cron="0 0 4,10,16,22 * * ?")
	public void  run(){

		//统计日期
		Date statisDate = null;
		StringBuffer sb = new StringBuffer(); 
		try {			
			//取得当前日期yyyyMMdd
			statisDate = DateUtil.fomatDate(DateUtil.fomatDate(new Date(), DateUtil.sdfDays), DateUtil.sdfDays);
					
			/*List<TradeOffline> records = profitStatisService.selectNeedProfitList(null);
			//循环处理单笔交易分润
			for (TradeOffline record : records) {
				try{
					profitStatisService.saveOneOrderProfitCalculate(record);
				} catch(Exception e){
					sysLogService.recordSysTaskLog(TaskType.ProfitDayCalculate.getType(),
							statisDate, RunStatus.Fail.getType(),
							sb.append("订单号:").append(record.getId()).append("计算分润失败！").toString());
					return;
				}
			}*/
			logger.info(sb.append("计划任务:")
					.append(TaskType.ProfitDayCalculate.getName())
					.append("统计日期:").append(DateUtil.fomatDate(statisDate, DateUtil.sdfDays)).append("执行成功！"));
			sysLogService.recordSysTaskLog(TaskType.ProfitDayCalculate.getType(),
					statisDate, RunStatus.Success.getType(),
					sb.append("分润计算").append(DateUtil.fomatDate(new Date(), DateUtil.sdfTime)).append("执行成功！").toString());
		}		
		 catch (Exception e) { 
			e.printStackTrace();
			logger.info(sb.append("计划任务:")
					.append(TaskType.ProfitDayCalculate.getName())
					.append("统计日期：").append(DateUtil.fomatDate(statisDate, DateUtil.sdfDays)).append("执行失败！"));			
			logger.error(e);
		}
	}

}
